﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Windows;
using Silvermoon;
using Passwords.Models;
using Silvermoon.UI;
using Silvermoon.Images;
using Silvermoon.Controls.Forms;
using Passwords.Biz;

namespace Passwords.Pages
{
    public abstract class ItemPickerPage : Page
    {
        #region fields
        protected readonly Command OKCommand = new Command(RoutingStrategy.Direct);
        protected readonly Command CancelCommand = new Command(RoutingStrategy.Direct);

        private Toolbar toolbar;
        private PasswordItem passwordItem;

        #endregion
        #region ctor dtor
        public ItemPickerPage()
            : base()
        {
            Styles = StylePool.Metro;
            Style = StylePool.EditWindow;
            OKCommand.Executed += OnOK;
            CancelCommand.Executed += OnCancel;
            toolbar = new Toolbar();
            //toolbar.Buttons.Add(new Button { Content = "Done", Command = OKCommand, Width = 200 });
            //toolbar.Buttons.Add(new Button { Content = "Cancel", Command = CancelCommand, Width = 200 });

            toolbar.Buttons.Add(new ToolbarButton { Content = SysImages.Ok, Command = OKCommand, Enabled = false });
            toolbar.Buttons.Add(new ToolbarButton { Content = SysImages.RecycleBin });
            toolbar.Buttons.Add(new ToolbarButton { Content = SysImages.Cancel, Command = CancelCommand });
            this.Toolbar = toolbar;

            toolbar.Buttons[1].Tap += new EventHandler<TapEventArgs>(OnRecycle);
        }

        public bool IsSaveEnabled
        {
            get { return toolbar.Buttons[0].Enabled; }
            set { if (toolbar.Buttons != null && toolbar.Buttons.Count > 0) toolbar.Buttons[0].Enabled = value; }
        }

        void OnRecycle(object sender, TapEventArgs e)
        {
            if (passwordItem != null)
            {
                MessageDialog.Show("Delete " + passwordItem.Name + "?", "", "Delete", "Cancel",
                    () =>
                    {
                        passwordItem.MakeEmpty();
                        GoBack();
                    }, null);
            }
        }

        protected override void Dispose(bool dispose)
        {
            OKCommand.Executed -= OnOK;
            CancelCommand.Executed -= OnCancel;
            base.Dispose(dispose);
        }

        #endregion
        #region props


        public PasswordItem PasswordItem
        {
            get { return passwordItem; }
            set
            {
                if (passwordItem != value)
                {
                    passwordItem = value;
                    OnPasswordItemChanged(value);
                }
            }
        }


        #endregion
        #region methods

        protected virtual void OnOK(object sender, CommandEventArgs e)
        {
            passwordItem = null; // don't alow the OnBack method to undo.
            GoBack();
        }

        protected override void OnBack(HandledEventArgs e)
        {
            if (passwordItem != null) passwordItem.Undo();
            base.OnBack(e);
        }

        protected virtual void OnCancel(object sender, EventArgs e)
        {
            GoBack();
        }

        protected virtual void OnPasswordItemChanged(PasswordItem item)
        {
            IsSaveEnabled = item != null && item.IsNewOrModified;
            toolbar.Buttons[1].Enabled = item != null && item.IsEmpty == false;
        }

        public override void OnExitApplication()
        {
            OnOK(this, null);
        }

        #endregion
    }
}
